package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.interceptor.RequireLogin;
import cn.wolfcode.wolf2w.interceptor.UserParam;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private ITravelCommentService travelCommentService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @GetMapping("query")
    public Object query(TravelQuery qo){
          Page<Travel> page = travelService.query(qo);
          return JsonResult.success(page);
    }

    @GetMapping("detail")
    public Object detail(String id){
        Travel travel = travelService.get(id);
        return JsonResult.success(travel);
    }



    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    @GetMapping("input")
    public Object input(String id){
        //查询目的地下拉框数据，回显当前对象数据
        List<Destination> list = destinationService.list();
        HashMap<String, Object> map = new HashMap<>();
        if(StringUtils.hasLength(id)){
            Travel travel = travelService.get(id);
            map.put("tv",travel);
        }
        map.put("dests",list);
        return JsonResult.success(map);
    }

    @RequireLogin
    @PostMapping("saveOrUpdate")
    public Object saveOrUpdate(Travel travel, HttpServletRequest request){
        //获取当前用户对象
        String token = request.getHeader("token");
        UserInfo userInfo = userInfoRedisService.getUserInfo(token);
        //在游记中设置当前用户对象id
        travel.setUserId(userInfo.getId());
        travelService.saveOrUpdate(travel);
        //返回页面需要的新增/编辑游记
        return JsonResult.success(travel.getId());
    }


    //在接口形式参数中声明用户对象，springmvc自动将当前登录用户对象注入过来
    @GetMapping("/getUser")
    public Object getUser(@UserParam UserInfo userInfo){
        //获取当前登录用户对象
        return JsonResult.success(userInfo);
    }

    //如果需要做用户编辑
    //此时可以使用@UserParam注解进行区分
    //参数中贴有这个注解表示需要使用自定义的参数注解解析器
    //参数中没有贴这个注解表示使用springmvc默认参数解析器
    @GetMapping("/userUpdate")
    public Object updateUser(UserInfo userInfo){
        //获取当前的登录用户对象
        return JsonResult.success(userInfo);
    }




    @RequireLogin
    @PostMapping("commentAdd")
    public Object commentAdd(TravelComment travelComment,@UserParam UserInfo userInfo){
        //  将源对象的属性拷贝到目标对象，前提：属性名词必须一样才可以拷贝
       //将用户属性设置到评论表中
        BeanUtils.copyProperties(userInfo,travelComment);
         travelComment.setUserId(userInfo.getId());
         travelCommentService.save(travelComment);
         return JsonResult.success();
    }

    @GetMapping("comments")
    public Object comments(String travelId){

        List<TravelComment> list = travelCommentService.findByTravelId(travelId);
        return JsonResult.success(list);
    }


}
